<?php

namespace App\Http\Resources\Trip\Orders;

use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class OrderFlightResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $flight                    = parent::toArray($request);
        $start                     = Carbon::parse($flight['start_at']);
        $arrive                    = Carbon::parse($flight['arrive_at']);
        $flight["start_date"]      = $start->toDateString();
        $flight["start_time"]      = $start->format("H:i");
        $flight["arrive_date"]     = $arrive->toDateString();
        $flight["arrive_time"]     = $arrive->format("H:i");
        $flight["start_time_diff"] = Carbon::now()->lessThan(Carbon::parse($flight['start_at'])) ?
            Carbon::now()->diffInMinutes($flight['start_at']) : 0;
        unset($flight['start_at'], $flight['arrive_at']);
        return $flight;
    }
}
